const { add, getAll, remove, getCount } = require('../../utils/dbTools');
const { getUploadedUrl } = require('../../utils/upload');
const { createLog } = require('../../utils/util');

// 数据表名称
const tableName = 'banner';

// 请求数据总条数
const bannerTotal = async (req, res) => {
    const { bus_id } = req.query;
    const output = {
        code: 200,
        msg: '操作成功'
    }
    if (!bus_id) {
        output.code = 500;
        output.msg = '参数错误:缺少参数:bus_id';
        return res.send(output)
    }
    const result = await getCount(tableName, { bus_id, status: 1 });
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取轮播图列表
 * 
*/
const bannerList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size, bus_id } = req.query;
    if (!bus_id) {
        output.code = 500;
        output.msg = '参数错误:缺少参数:bus_id';
        return res.send(output)
    }
    const params = { bus_id, status: 1 };
    if (page && size) {
        params.page = page;
        params.size = size;
    }
    const result = await getAll(tableName, params);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 轮播图新增
 * 
*/
const bannerAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { title, url, bus_id, status } = req.body;
    const { file } = req;
    if (!file || !bus_id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数:title,file,bus_id";
        return res.send(output);
    }
    const data = { bus_id };
    if (title) data.title = title
    if (url) data.url = url
    if (status) data.status = status
    // 获取上传完成的图片地址
    data.img = getUploadedUrl(file);
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    createLog(req, { type: 16, remark: "新增轮播图" });

    res.send(output);
}

/**
 * 更新轮播图
 * */
const bannerUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id, title, url, bus_id, status } = req.body;
    const { file } = req;
    if (!id || !bus_id) {
        output.code = 500;
        output.msg = "参数错误:缺少参数id, bus_id";
        return res.send(output);
    }
    const data = { bus_id }
    if (title) data.title = title;
    if (url) data.url = url;
    if (status) data.status = status
    // 获取轮播图路径地址
    if (file) data.img = getUploadedUrl(file);
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "更新失败";
        return res.send(output);
    }
    createLog(req, { type: 17, remark: "更新轮播图" });

    res.send(output);
}

/**
 * 删除轮播图
 * 
*/
const bannerRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    createLog(req, { type: 7, remark: "删除轮播图" });

    res.send(output);
}

module.exports = {
    bannerList,
    bannerAdd,
    bannerUpdate,
    bannerRemove,
    bannerTotal
}